#!/bin/sh
DBHOST=`hostname`
port=""
source /home/mysql/.bash_profile
#in git
get_base() {
  if [ $1 -eq 3306 ]; then
  basedir="/u01/mysql"
  elif [ $1 -eq 3307 ]; then
  basedir="/u01/mysql2"
  elif [ $1 -eq 3406 ]; then
  basedir="/u01/mysql2"
  elif [ $1 -eq 3506 ]; then
  basedir="/u01/mysql3"
  elif [ $1 -eq 3606 ]; then
  basedir="/u01/mysql4"
  else
  echo "no port set"
  fi
}


ports=`ps -ef |grep mysqld|grep port=|awk -F"port=" '{print $NF}'|grep -v awk|awk '{print $1}'|grep -v awk`
[ -z $ports ] && echo "no mysql instance" && exit 0
insnum=`echo "$ports"|wc -l`
inslog="/tmp/tmp_ins`date +%Y%m%d`"

if  [ $1 = "record" -a $insnum -gt 0 ] ;then
     [ $insnum -eq 0  ]  && echo "no mysql " && exit 9;
   echo "${ports[@]}" | tr ' ' '\n' > $inslog
    [ $? -eq 0 ] && echo "record success"
fi

if [ $1 = "check" ];then
  #oldnum=`cat $inslog|wc -l`
  #[ $oldnum -ne $insnum  ]  && echo "ERROR" && echo "some Instances not start"
  for port in `cat $inslog`
    do
      tmpnum=`ps -ef|grep mysqld|grep $port|grep -v grep|wc -l`
      if [ $tmpnum -eq 0 ];then 
        echo "ERROR,$port not start"
      fi
      echo "all started"
  done
fi

if [ $1 = "slave" ];then
  [ $insnum -eq 0  ] && echo "no mysql " && exit 9;
  for port in `cat $inslog`
     do
        /u01/mysql/bin/mysql -h127.0.0.1 -uroot -P$port  -e 'start slave';
     done
   echo "all start slave"
fi

if [ $1 = "stopslave" ];then
  [ $insnum -eq 0  ] && echo "no mysql " && exit 9;
  for port in `cat $inslog`
     do
        /u01/mysql/bin/mysql -h127.0.0.1 -uroot -P$port  -e 'stop slave';
     done
   echo "all start slave"
fi

if [  $1 = "readonly" ];then
  [ $insnum -eq 0  ] && echo "no mysql " && exit 9;
  nownum=0
  for port in $ports
     do
     tmpnum=`/u01/mysql/bin/mysql -h127.0.0.1 -uroot -P$port -e  "show variables like 'read_only';"|grep ON|wc -l`
     [ $tmpnum -eq 1 ] &&  let nownum+=1
  done
[ $nownum -lt $insnum ] && echo "ERROR"   && echo "some are not read_only!!"  && exit 99
echo "all are read_only"
fi


if [ $1 = "killback" ];then
   pids=`ps -ef|grep -E 'mysqldump|xtrabackup|tar4ibd|pbzip'|grep -v '3306|3406|3506|3606|3706'|grep -v grep|awk '{print $2}'`
   if [ `echo $pids|wc -l ` -gt 0 ];then
     for pid in $pids ;do kill -9 $pid ;done
   fi 
   echo "backup have been killed"
fi


if [ $1 = "shutdown" ];then
  [ $insnum -eq 0  ] && echo "no mysql " && exit 9;
  for port in $ports
     do
        /u01/mysql/bin/mysqladmin -h127.0.0.1 -uroot -P$port  shutdown;
  done
  nownum=`ps -ef|grep mysqld|grep -v grep|wc -l`
  while [ $nownum -gt 0 ]
  do
    sleep 4;
    nownum=`ps -ef|grep mysqld|grep -v grep|wc -l`
  done
  echo "all shutdown"
fi

if [ $1 = "setreadonly" ];then
  [ $insnum -eq 0  ] && echo "no mysql " && exit 9;
  for port in $ports
     do
        /u01/mysql/bin/mysql -h127.0.0.1 -uroot -P$port -e "set global reaonly;";
  done
fi

if [ $1 = "startup" ];then
  oldnum=`cat $inslog|wc -l`
  if [ $insnum -lt $oldnum ];then
    for port in `cat $inslog`
    do
       tmpnum=`ps -ef|grep mysqld|grep $port|grep -v grep|wc -l`
       get_base $port
       [ $tmpnum -eq 0 ]  &&  /u01/mysql/bin/mysqld_unsafe  --defaults-file=${basedir}/my.cnf --read-only=1  --user=mysql &
    done
    sleep 3 && insnum=`ps -ef |grep mysqld|grep sock|grep -v grep|wc -l`
    while [  $insnum -ne $oldnum  ]
    do 
      sleep 3
      insnum=`ps -ef |grep mysqld|grep sock|grep -v grep|wc -l`
    done
    echo "all startup"
  else 
    echo "There are full Instances"
  fi
fi



if [ $1 = "checkslave" ];then
  [ $insnum -eq 0  ] && echo "no mysql " && exit 9;
  for port in $ports
     do
        /u01/mysql/bin/mysql -h127.0.0.1 -uroot -P$port -e "show slave status\G";
  done
fi
